//
//  HomeTableViewController.swift
//  DSWeibo
//
//  Created by liuzhu on 2018/10/29.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

let XMGHomeReuseIdentifier = "XMGHomeReuseIdentifier"
class HomeTableViewController: BaseTableViewController {

    var statuses:[Status]?{
        didSet{
            tableView.reloadData()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin{
            return
        }
        setupNav()
        NotificationCenter.default.addObserver(self, selector: #selector(change), name: NSNotification.Name(rawValue: XMGPopoverAnimatorWillShow), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(change), name: NSNotification.Name(rawValue: XMGPopoverAnimatorWillDismiss), object: nil)
        
        //注册一个cell
        tableView.register(StatusTableViewCell.self, forCellReuseIdentifier: XMGHomeReuseIdentifier)
        
        tableView.estimatedRowHeight = 200
        tableView.rowHeight = UITableView.automaticDimension
        loadData()
        
    }

    private func loadData(){
        Status.loadStatuses { (models, error) in
            self.statuses = models
        }
    }
    
    private func setupNav()
    {
        //左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_friendattention",target: self,action:#selector(leftItemClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "navigationbar_pop",target: self,action: #selector(rightItemClick))
        //标题按钮
        let titleBtn = TitleButton()
        titleBtn.setTitle("2018-11-10 ", for: UIControl.State.normal)
        titleBtn.addTarget(self, action: #selector(titleClick), for: UIControl.Event.touchUpInside)
        navigationItem.titleView = titleBtn;
    }
    
    @objc private func leftItemClick(){
        NSLog(#function)
    }
    
    @objc private func rightItemClick(){
        NSLog(#function)
        let sb = UIStoryboard(name: "QRCodeViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        present(vc!, animated: true, completion: nil)
    }
    
    @objc private func titleClick(btn:TitleButton){
        NSLog(#function)
        //弹出菜单
        let sb = UIStoryboard(name: "PopoverViewController", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        //vc?.transitioningDelegate = self
        vc?.transitioningDelegate=popoverAnimator
        vc?.modalPresentationStyle = UIModalPresentationStyle.custom
        present(vc!, animated: true, completion: nil)
    }
    
    @objc private func change(){
        let titleBtn = navigationItem.titleView as!TitleButton
        titleBtn.isSelected = !titleBtn.isSelected
    }
    
    private lazy var popoverAnimator:PopoverAnimator = {
        let pa = PopoverAnimator()
        pa.presentedFrame = CGRect(x: 10, y: 90, width: UIScreen.main.bounds.size.width-20, height: UIScreen.main.bounds.size.height-180)
        return pa
    }()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

extension HomeTableViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: XMGHomeReuseIdentifier, for: indexPath) as! StatusTableViewCell
        
        let status = statuses![indexPath.row]
        cell.status = status
        return cell
    }
    
//    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//        return 200
//    }
}

